export default {
  namespaced: true,
  state: {
    todoList: []
  },
  getters: {
    getAllList: (state) => state.todoList
  },
  mutations: {
    addTodo(state, payload) {
      state.todoList.push(payload)
    },
    delTodo(state, id) {
      state.todoList = state.todoList.filter(item => item.id != id)
    },
    changeState(state, id) {
      state.todoList = state.todoList.map(item => {
        if (item.id == id) {
          item.state = !item.state;
        }
        return item;
      })
    },
    editTodo(state, payload) { // payload 应该包含 id 和 text
      state.todoList = state.todoList.map(item => {
        if (item.id == payload.id) {
          item = {
            ...item,
            ...payload
          };
        }
        return item;
      })
    }
  }
}